package module_five.task03.serverModel;

import module_five.task03.ClientModel.Student;
import java.io.*;
import java.util.HashSet;
import java.util.Iterator;


public class ServerStudentManager {

    /**
     * 自定义成员方式, 查看所有的学员信息
     * @return
     */
    private HashSet<Student> selectAll() throws IOException, ClassNotFoundException {

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./students.txt"));
        HashSet<Student> students = (HashSet<Student>) ois.readObject();
        if (null == students) { //
            students = new HashSet<>();
        }
        return students;
    }

    /**
     * 自定义成员方法, 添加一个学员
     * @param student
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean addStudent(Student student) throws IOException, ClassNotFoundException {

        HashSet<Student> students = selectAll();
        boolean b1 = students.add(student);
        saveStudents(students);
        return b1;
    }

    /**
     * 保存学员信息到文件中; 每次增加或删除学员后, 需要重新保存学员信息
     * @param students
     * @throws IOException
     */
    private void saveStudents(HashSet<Student> students) throws IOException {

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./students.txt"));
        oos.writeObject(students);
    }

    /**
     * 根据学员id查询学员信息
     * @param id
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public Student selectStudent(int id) throws IOException, ClassNotFoundException {

        HashSet<Student> students = selectAll();
        Student select = null;
        for (Student student : students) {
            if (student.getId() == id) {
                select = student;
                break;
            }
        }

        return select;
    }

    /**
     * 根据学员id删除学员信息
     * @param id
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean deleteStudent(int id) throws IOException, ClassNotFoundException {

        HashSet<Student> students = selectAll();

        Iterator<Student> iterator = students.iterator();
        boolean isSuccess = false;
        while (iterator.hasNext()) {
            Student next = iterator.next();
            if (next.getId() == id) {
                iterator.remove();
                isSuccess = true;
                break;
            }
        }

        saveStudents(students);
        return isSuccess;
    }

    /**
     * 根据学员id找到学员, 并修改学员的信息
     * @param student
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean modifyStudent(Student student) throws IOException, ClassNotFoundException {

        HashSet<Student> students = selectAll();

        Iterator<Student> iterator = students.iterator();
        boolean isSuccess = false;
        while (iterator.hasNext()) {
            Student next = iterator.next();
            if (next.getId() == student.getId()) {
                next.setName(student.getName());
                next.setAge(student.getAge());
                isSuccess = true;
                break;
            }
        }

        saveStudents(students);
        return isSuccess;
    }

    public ServerStudentManager() {

        File file = new File("./students.txt");
        if (!file.exists()) {
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./students.txt"));
                oos.writeObject(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        ServerStudentManager ssm = new ServerStudentManager();
        HashSet<Student> students = ssm.selectAll();
        System.out.println(students);
    }
}
